import { Controller } from "../../mvc/core/Controller";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { DataEvent } from "../events/DataEvent";
import { WindowEvent } from "../../component/window/WindowEvent";
import { IMagicWeaponModule } from "../../../modules/interfaces/IMagicWeaponModule";
import { GModuleEvent } from "../mvc/GModuleEvent";
import { GameProxy } from "../mvc/GameProxy";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SAttribute } from "../../../Message/Public/SAttribute";
import { UpgradeVo } from "../view/magicWeapon/data/UpgradeVo";
import { IdentifyVo } from "../view/magicWeapon/data/IdentifyVo";
import { ShiftVo } from "../view/magicWeapon/data/ShiftVo";
import { RefreshVo } from "../view/magicWeapon/data/RefreshVo";
import { ItemData } from "../resource/info/ItemData";
import { RefreshAmuletReplaceVo } from "../view/magicWeapon/data/RefreshAmuletReplaceVo";
import { RefreshPreViewVo } from "../view/magicWeapon/RefreshPreViewVo";
import { ComposeAmuletVo } from "../view/magicWeapon/data/ComposeAmuletVo";
import { EResetTimes } from "../../../Message/Public/EResetTimes";
import { TaskInfo } from "../model/TaskInfo";
import { AIManager } from "../scene/ai/AIManager";
import { ETaskStatus } from "../../../Message/Game/ETaskStatus";
import { Window } from "../../component/window/Window";
import { Language } from "../../../extend/language/Language";
import { MagicWeaponGuideTaskIcon } from "../view/magicWeapon/MagicWeaponGuideTaskIcon";
import { GameController } from "../mvc/GameController";
import { DisplayUtil } from "../../common/DisplayUtil";
import { ResFileConst } from "../resource/ResFileConst";
import { IView } from "../../mvc/interfaces/IView";
import { MagicWeaponModule } from "../../../modules/MagicWeaponModule";
import { GuidePageWindow } from "../view/common/guide/GuidePageWindow";
import { MagicWeaponConfig } from "../resource/MagicWeaponConfig";
import { MagicWeaponBtn } from "../../../ResFileConst/MagicWeaponBtn";
type int = number;
//class MagicWeaponController
    
    export  class MagicWeaponController extends Controller
    {
       

        /* internal  */removeGuideEvents(): void
        {
            NetDispatcher.removeCmdListener(ServerCommand.TaskGetRes, this.taskGetHandler);
            NetDispatcher.removeCmdListener(ServerCommand.TaskEndRes, this.taskEndHandler);
            NetDispatcher.removeCmdListener(ServerCommand.TaskListRes, this.checkTaskDoingHandler);
            NetDispatcher.removeCmdListener(ServerCommand.TaskAdd, this.checkTaskDoingHandler);
            NetDispatcher.removeCmdListener(ServerCommand.CanGetListAdd, this.checkTaskDoingHandler);
            NetDispatcher.removeCmdListener(ServerCommand.CanGetListRefresh, this.checkTaskDoingHandler);
            NetDispatcher.removeCmdListener(ServerCommand.TaskUpdate, this.taskUpdateHandler);
            Dispatcher.removeEventListener(EventName.WindowClosed, this.winClosedHandler);
            return;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.MagicWeaponUpgrade, this.onUpgradeHandler);
            Dispatcher.addEventListener(EventName.MagicWeaponUpgradeSucceed, this.onUpgradeSucceed);
            Dispatcher.addEventListener(EventName.MagicWeaponIdentify, this.onIdentifyHandler);
            Dispatcher.addEventListener(EventName.MagicWeaponIdentifySucceed, this.onIdentifySucceed);
            Dispatcher.addEventListener(EventName.MagicWeaponResolve, this.onResolveHandler);
            Dispatcher.addEventListener(EventName.MagicWeaponResolveSucceed, this.onResolveSucceed);
            Dispatcher.addEventListener(EventName.MagicWeaponShift, this.onShiftHandler);
            Dispatcher.addEventListener(EventName.MagicWeaponShiftSucceed, this.onShiftSucceed);
            Dispatcher.addEventListener(EventName.MagicWeaponRefresh, this.onRefreshHandler);
            Dispatcher.addEventListener(EventName.MagicWeaponRefreshSucceed, this.onRefreshSucceed);
            Dispatcher.addEventListener(EventName.MagicWeaponRefreshReplace, this.onRefreshReplaceHandler);
            Dispatcher.addEventListener(EventName.MagicWeaponRefreshReplaceSucceed, this.onRefreshReplaceSucceed);
            Dispatcher.addEventListener(EventName.MagicWeaponRefreshPreView, this.onRefreshPreViewHandler);
            Dispatcher.addEventListener(EventName.MagicWeaponRefreshPreViewSucceed, this.onRefreshPreViewSucceedHandler);
            Dispatcher.addEventListener(EventName.MagicWeaponComposite, this.onCompositeHandler);
            Dispatcher.addEventListener(EventName.MagicWeaponCompositeSucceed, this.onMagicWeaponCompositeSucceedHandler);
            Dispatcher.addEventListener(EventName.MagicWeaponResetUpgradeTime, this.onResetUpgradeTime);
            Dispatcher.addEventListener(EventName.MagicWeaponAddScore, this.onMagicWeaponAddScoreHandler);
            Dispatcher.addEventListener(EventName.MagicWeaponShowView, this.onShowViewHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateAmuletGoldIdentifyTimes, this.identifyTimes);
            NetDispatcher.addCmdListener(ServerCommand.AmuletHoleNumUpdateCommand, this.onAmuletHoleNumUpdateHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateUpgradeAmuletScoreTimes, this.onUpgradeAmuletScoreTimesHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskGetRes, this.taskGetHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskEndRes, this.taskEndHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskListRes, this.checkTaskDoingHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskAdd, this.checkTaskDoingHandler);
            NetDispatcher.addCmdListener(ServerCommand.CanGetListAdd, this.checkTaskDoingHandler);
            NetDispatcher.addCmdListener(ServerCommand.CanGetListRefresh, this.checkTaskDoingHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskUpdate, this.taskUpdateHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateAmuletGoldIdentifyTimes, this.identifyTimes);
            Dispatcher.addEventListener(EventName.WindowClosed, this.winClosedHandler);
            Dispatcher.addEventListener(EventName.Scene_Update, this.sceneUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskListRes, this.taskListGotHandler);
            Dispatcher.addEventListener(EventName.MagicWeaponShowPageGuide, this.showPageGuideHandler);
            return;
        }

        /* internal  */onFahunInfoUpdateHandler(arg1: DataEvent): void
        {
            if (isViewShow) 
            {
                this._magicWeaponModule.updateFahunList(cache.fahun.fahunItemInfoArray);
            }
            return;
        }

        /* internal  */showModule(arg1: WindowEvent): void
        {
            cache.magicWeapon.isActiveInEffect();
            this.updateMagicWeapon();
            this.updateMoney();
            IMagicWeaponModule(view).showModule();
            IMagicWeaponModule(view).updateFabaoData(cache.pack.rolePackCache.magicWeaponItems, cache.role.entityInfo.sex);
            Dispatcher.dispatchEvent(new DataEvent(EventName.FahunOperView));
            view.addEventListener(GModuleEvent.Event_unDress, this.onUnDress);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.updateMoney);
            NetDispatcher.addCmdListener(ServerCommand.BackpackDataChange, this.serverBagDataChangehandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            NetDispatcher.addCmdListener(ServerCommand.RolepackDataChange, this.serverBagDataChangehandler);
            NetDispatcher.addCmdListener(ServerCommand.RolePackItemsChange, this.serverBagDataChangehandler);
            NetDispatcher.addCmdListener(ServerCommand.AmuletHoleNumUpdateCommand, this.serverBagDataChangehandler);
            NetDispatcher.addCmdListener(ServerCommand.MagicWeaponPackDataChange, this.serverBagDataChangehandler);
            NetDispatcher.addCmdListener(ServerCommand.MagicWeaponPackItemsChange, this.serverBagDataChangehandler);
            Dispatcher.addEventListener(EventName.FahunInfoUpdate, this.onFahunInfoUpdateHandler);
            return;
        }

        /* internal  */closeModule(arg1: WindowEvent): void
        {
            IMagicWeaponModule(view).clean();
            view.removeEventListener(GModuleEvent.Event_unDress, this.onUnDress);
            NetDispatcher.removeCmdListener(ServerCommand.MoneyUpdate, this.updateMoney);
            NetDispatcher.removeCmdListener(ServerCommand.BackpackDataChange, this.serverBagDataChangehandler);
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            NetDispatcher.removeCmdListener(ServerCommand.RolepackDataChange, this.serverBagDataChangehandler);
            NetDispatcher.removeCmdListener(ServerCommand.RolePackItemsChange, this.serverBagDataChangehandler);
            NetDispatcher.removeCmdListener(ServerCommand.AmuletHoleNumUpdateCommand, this.serverBagDataChangehandler);
            NetDispatcher.removeCmdListener(ServerCommand.MagicWeaponPackDataChange, this.serverBagDataChangehandler);
            NetDispatcher.removeCmdListener(ServerCommand.MagicWeaponPackItemsChange, this.serverBagDataChangehandler);
            return;
        }

        /* internal  */updateMagicWeapon(): void
        {
            cache.magicWeapon.updateMagicWeapon();
            IMagicWeaponModule(view).updateMagicWeapon();
            return;
        }

        /* internal  */updateMoney(arg1: Object=null): void
        {
            if (_view && !_view.isHide) 
            {
                IMagicWeaponModule(view).updateMoney();
            }
            return;
        }

        /* internal  */onUnDress(arg1: DataEvent): void
        {
            GameProxy.roleProxy.undress_async(arg1.data.uid, arg1.data.index);
            return;
        }

        /* internal  */identifyTimes(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SAttribute;
            if (loc1) 
            {
                cache.magicWeapon.identifyTimes = loc1.value;
                cache.magicWeapon.resetTimes = int(loc1.valueStr);
            }
            if (_view && !_view.isHide) 
            {
                IMagicWeaponModule(view).updateMagicWeapon();
            }
            return;
        }

        /* internal  */CultureAmuletCrit(arg1: MessageBlock): void
        {
            IMagicWeaponModule(view).playEffect();
            return;
        }

        /* internal  */serverBagDataChangehandler(arg1: Object=null): void
        {
            if (_view && !_view.isHide) 
            {
                this.updateMagicWeapon();
                IMagicWeaponModule(view).updateFabaoData(cache.pack.rolePackCache.magicWeaponItems, cache.role.entityInfo.sex);
            }
            return;
        }

        /* internal  */onAmuletHoleNumUpdateHandler(arg1: int): void
        {
            if (_view && !_view.isHide) 
            {
                IMagicWeaponModule(view).updateFabaoHoleNum(arg1);
            }
            return;
        }

        /* internal  */onUpgradeAmuletScoreTimesHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SAttribute;
            cache.magicWeapon.upgradeAmuletScoreTimes = loc1.value;
            if (_view && !_view.isHide) 
            {
                IMagicWeaponModule(view).updateMagicWeapon();
            }
            return;
        }

        /* internal  */onUpgradeHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data.vo as UpgradeVo;
            GameProxy.magicWeaponProxy.upgradeAmulet(loc1);
            return;
        }

        /* internal  */onIdentifyHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data.vo as IdentifyVo;
            GameProxy.magicWeaponProxy.identifyAmulet(loc1);
            return;
        }

        /* internal  */onResolveHandler(arg1: DataEvent): void
        {
            GameProxy.magicWeaponProxy.decomposeAmulet(arg1.data.items);
            return;
        }

        /* internal  */onShiftHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data.vo as ShiftVo;
            GameProxy.magicWeaponProxy.shiftAmulet(loc1);
            return;
        }

        /* internal  */onIdentifySucceed(arg1: DataEvent): void
        {
            var loc1=arg1.data.resultPlayerItem;
            IMagicWeaponModule(view).identifySucceed(loc1);
            return;
        }

        /* internal  */onResolveSucceed(arg1: DataEvent): void
        {
            IMagicWeaponModule(view).resolveSucceed();
            return;
        }

        /* internal  */onUpgradeSucceed(arg1: DataEvent): void
        {
            var loc1=arg1.data.resultPlayerItem;
            var loc2=int(arg1.data.update);
            var loc3=Boolean(arg1.data.isCri);
            IMagicWeaponModule(view).upgradeSucceed(loc1, loc2, loc3);
            return;
        }

        /* internal  */onShiftSucceed(arg1: DataEvent): void
        {
            IMagicWeaponModule(view).shiftSucceed();
            return;
        }

        /* internal  */onRefreshHandler(arg1: DataEvent): void
        {
            GameProxy.magicWeaponProxy.refreshAmulet(arg1.data as RefreshVo);
            return;
        }

        /* internal  */onRefreshSucceed(arg1: DataEvent): void
        {
            IMagicWeaponModule(view).refreshAndReplaceSucceed(arg1.data as ItemData);
            return;
        }

        /* internal  */onRefreshReplaceHandler(arg1: DataEvent): void
        {
            GameProxy.magicWeaponProxy.refreshAmuletReplace(arg1.data as RefreshAmuletReplaceVo);
            return;
        }

        /* internal  */onRefreshReplaceSucceed(arg1: DataEvent): void
        {
            IMagicWeaponModule(view).refreshAndReplaceSucceed(arg1.data as ItemData, true);
            return;
        }

        /* internal  */onRefreshPreViewHandler(arg1: DataEvent): void
        {
            GameProxy.magicWeaponProxy.refreshPreView(arg1.data as RefreshPreViewVo);
            return;
        }

        /* internal  */onRefreshPreViewSucceedHandler(arg1: DataEvent): void
        {
            IMagicWeaponModule(view).refreshPreViewSucceed(arg1.data as int);
            return;
        }

        /* internal  */onCompositeHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as ComposeAmuletVo;
            GameProxy.magicWeaponProxy.composeAmulet(loc1);
            return;
        }

        /* internal  */onMagicWeaponCompositeSucceedHandler(arg1: DataEvent): void
        {
            IMagicWeaponModule(view).compositeSucceed();
            return;
        }

        /* internal  */onResetUpgradeTime(arg1: DataEvent): void
        {
            var loc1=EResetTimes.convert(EResetTimes._EResetTimesAmulet);
            GameProxy.roleProxy.resetTimes(loc1);
            return;
        }

        /* internal  */onMagicWeaponAddScoreHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as string;
            GameProxy.magicWeaponProxy.upgradeAmuletScore(loc1);
            return;
        }

        /* internal  */onShowViewHandler(arg1: DataEvent): void
        {
            var loc1=null;
            if (cache.magicWeapon.isCanIdentifyMagicWeapon) 
            {
                loc1 = arg1.data as ItemData;
                view.show();
                IMagicWeaponModule(view).selectPanel(loc1);
            }
            return;
        }

        /* internal  */taskGetHandler(arg1: Object): void
        {
            var loc2=null;
            if (arg1 == null) 
            {
                return;
            }
            this.checkTaskDoingHandler(arg1);
            var loc1=arg1["task"] as TaskInfo;
            if (loc1 == null || loc1.sTask == null) 
            {
                return;
            }
            if (loc1.sTask.code == 2001016) 
            {
                loc2 = loc1.getTargetByIndex(0);
                AIManager.onAutoPathAIControl(loc2);
                return;
            }
            if (MagicWeaponController.CompleteAtuoPathCodes.indexOf(loc1.sTask.code) >= 0 && loc1.status == ETaskStatus._ETaskStatusHadCompleted) 
            {
                loc2 = loc1.getTargetByIndex(0);
                AIManager.onAutoPathAIControl(loc2);
            }
            return;
        }

        /* internal  */taskEndHandler(arg1: Object): void
        {
            var loc2=null;
            if (arg1 == null) 
            {
                return;
            }
            this.checkTaskDoingHandler(arg1);
            var loc1=arg1["task"] as TaskInfo;
            if (loc1 == null || loc1.sTask == null) 
            {
                return;
            }
            if (loc1.sTask.code == 2001016) 
            {
                loc2 = loc1.getTargetByIndex(0);
                AIManager.onAutoPathAIControl(loc2);
            }
            return;
        }

        /* internal  */taskUpdateHandler(arg1: Object): void
        {
            var loc2=null;
            var loc3=null;
            var loc4=0;
            if (arg1 == null) 
            {
                return;
            }
            var loc1=arg1 as Array<any>;
            if (loc1 == null || loc1.length == 0) 
            {
                return;
            }
            var loc5=0;
            var loc6=loc1;
            for(loc3 of loc6) 
            {
                if (!loc3.sTask) 
                {
                    continue;
                }
                loc4 = loc3.sTask.code;
                if (MagicWeaponController.CompleteAtuoPathCodes.indexOf(loc4) >= 0 && loc3.status == ETaskStatus._ETaskStatusHadCompleted) 
                {
                    loc2 = loc3.getTargetByIndex(0);
                    AIManager.onAutoPathAIControl(loc2);
                    break;
                }
                if (!(MagicWeaponController.CompleteDelayAutoPathCodes.indexOf(loc4) >= 0 && loc3.status == ETaskStatus._ETaskStatusHadCompleted)) 
                {
                    continue;
                }
                this._delayTask = loc3;
                return;
            }
            return;
        }

        /* internal  */winClosedHandler(arg1: DataEvent): void
        {
            if (this._delayTask == null || MagicWeaponController.CompleteDelayAutoPathCodes.indexOf(this._delayTask.sTask.code) < 0) 
            {
                return;
            }
            var loc1=arg1.data as Window;
            if (loc1 == null) 
            {
                return;
            }
            if (loc1.winTitleName == Language.getString(30003) && this._delayTask.sTask.code == 2001021) 
            {
                AIManager.onAutoPathAIControl(this._delayTask.getTargetByIndex(0));
                this._delayTask = null;
            }
            else if (loc1.winTitleName == Language.getString(27650)) 
            {
                AIManager.onAutoPathAIControl(this._delayTask.getTargetByIndex(0));
                this._delayTask = null;
            }
            return;
        }

        /* internal  */checkTaskDoingHandler(arg1: Object): void
        {
            if (this._msgNum < 2) 
            {
                return;
            }
            this.checkAndShowModuleIcon();
            var loc1=cache.task.getCanGetTaskByCode(2001016);
            if (loc1 == null) 
            {
                loc1 = cache.task.getDoingTaskByCode(2001016);
            }
            if (loc1 == null) 
            {
                loc1 = cache.task.getCanGetTaskByCode(2001015);
            }
            if (loc1 == null) 
            {
                loc1 = cache.task.getDoingTaskByCode(2001015);
            }
            if (loc1 == null) 
            {
                loc1 = cache.task.getCanGetTaskByCode(2001017);
            }
            if (loc1 == null) 
            {
                loc1 = cache.task.getDoingTaskByCode(2001017);
            }
            if (loc1 == null) 
            {
                if (this._guideIcon != null) 
                {
                    this._guideIcon.hide();
                    this._guideIcon.clean();
                    this._guideIcon = null;
                }
                return;
            }
            if (this._guideIcon == null) 
            {
                this._guideIcon = new MagicWeaponGuideTaskIcon();
            }
            this._guideIcon.showMe(0, 0, loc1);
            return;
        }

        /* internal  */checkAndShowModuleIcon(): void
        {
            if (this._msgNum < 2) 
            {
                return;
            }
            var loc1=GameController.gameui.getGameui().navbarSprite;
            var loc2=DisplayUtil.getChildByPropety(loc1, "name", ResFileConst.MagicWeaponBtn);
            if (loc2 == null) 
            {
                return;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.GameUINavBarSetButtonVisible, {"name": MagicWeaponBtn, "value": cache.magicWeapon.isCanModuleIconShow()}));
            return;
        }

        protected /* override */ initView(): IView
        {
            if (this._magicWeaponModule == null) 
            {
                this._magicWeaponModule = new MagicWeaponModule();
                this._magicWeaponModule.addEventListener(WindowEvent.SHOW, this.showModule);
                this._magicWeaponModule.addEventListener(WindowEvent.CLOSE, this.closeModule);
                _view = this._magicWeaponModule;
            }
            return this._magicWeaponModule;
        }

        /* internal  */sceneUpdateHandler(arg1: DataEvent): void
        {
            Dispatcher.removeEventListener(EventName.Scene_Update, this.sceneUpdateHandler);
            var loc1;
            var loc2=((loc1 = this)._msgNum + 1);
            loc1._msgNum = loc2;
            this.checkTaskDoingHandler(null);
            this.checkGuideDone();
            return;
        }

        /* internal  */taskListGotHandler(arg1: Object): void
        {
            NetDispatcher.removeCmdListener(ServerCommand.TaskListRes, this.taskListGotHandler);
            var loc1;
            var loc2=((loc1 = this)._msgNum + 1);
            loc1._msgNum = loc2;
            this.checkTaskDoingHandler(null);
            this.checkGuideDone();
            return;
        }

        /* internal  */showPageGuideHandler(arg1: DataEvent): void
        {
            if (this._guideBook == null) 
            {
                this._guideBook = new GuidePageWindow(630, 400, Language.getString(80356));
            }
            if (!this._guideBook.isHide) 
            {
                this._guideBook.hide();
                return;
            }
            this._guideBook.data = MagicWeaponConfig.instance.GuideBookURLS;
            this._guideBook.show();
            return;
        }

        /* internal  */checkGuideDone(): void
        {
            if (this._msgNum < 2) 
            {
                return;
            }
            if (cache.magicWeapon.isAllGuideDone) 
            {
                this.removeGuideEvents();
            }
            return;
        }

        private static /* const */CompleteAtuoPathCodes: Array<any>=[2001016, 2001017, 2001019];

        private static /* const */CompleteDelayAutoPathCodes: Array<any>=[2001018, 2001021, 2001022];

        private /* var */_magicWeaponModule: IMagicWeaponModule;

        private /* var */_guideIcon: MagicWeaponGuideTaskIcon;

        private /* var */_guideBook: GuidePageWindow;

        private /* var */_msgNum: int=0;

        private /* var */_delayTask: TaskInfo;
    }
